Do a more thorough job keeping conflicted names unique.
authorrobertl <robertl@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Thu, 12 Jun 2003 15:36:01 +0000 (15:36 +0000)
committerrobertl <robertl@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Thu, 12 Jun 2003 15:36:01 +0000 (15:36 +0000)
gpsbabel/mkshort.c

index 00f41a2ebbe2cdf6efcde29208193d1d762c4bcd..f18b69eff48f255d79f0e7110e857b2b47d3e568 100644 (file)
@@ -67,38 +67,59 @@ mkshort_new_handle()
        return h;
 }
 
-char *
-mkshort_add_to_list(mkshort_handle *h, char *name)
+static 
+int
+is_unique(mkshort_handle *h, char *name)
 {
        queue *e, *t;
        int hash;
-       uniq_shortname *s = xcalloc(1, sizeof (uniq_shortname));
-       s->orig_shortname = xstrdup(name);
-       hash = hash_string(name);
 
+       hash = hash_string(name);
        QUEUE_FOR_EACH(&h->namelist[hash], e, t) {
                uniq_shortname *z = (uniq_shortname *) e;
-
                if (0 == case_ignore_strcmp(z->orig_shortname, name)) {
-                       size_t l = strlen(name);
-                       int dl;
-                       char tbuf[10];
+                       return 0;
+               }
+       }
+       return 1;
+}
 
-                       z->conflictctr++;
-                       dl = sprintf(tbuf, ".%d", z->conflictctr);
+static
+int
+add_to_hashlist(mkshort_handle *h, char *name)
+{
+       int hash;
 
-                       if (l + dl < h->target_len) {
-                               name = xrealloc(name, l + dl + 1);
-                               strcat(name, tbuf);
-                       }
-                       else {
-                               strcpy(&name[l-dl], tbuf);
-                       }
-                       break;
+       hash = hash_string(name);
+       uniq_shortname *s = xcalloc(1, sizeof (uniq_shortname));
+       s->orig_shortname = xstrdup(name);
+       ENQUEUE_TAIL(&h->namelist[hash], &s->list);
+}
+
+char *
+mkshort_add_to_list(mkshort_handle *h, char *name)
+{
+       while (!is_unique(h, name)) {
+               int dl;
+               char tbuf[10];
+               size_t l = strlen(name);
+               int hash = hash_string(name);
+               uniq_shortname *s = (uniq_shortname *) h->namelist[hash].next;
+
+               s->conflictctr++;
+
+               dl = sprintf(tbuf, ".%d", s->conflictctr);
+
+               if (l + dl < h->target_len) {
+                       name = xrealloc(name, l + dl + 1);
+                       strcat(name, tbuf);
+               }
+               else {
+                       strcpy(&name[l-dl], tbuf);
                }
        }
-       ENQUEUE_TAIL(&h->namelist[hash], &s->list);
-       h->depth[hash]++;
+
+       add_to_hashlist(h, name);
        return name;
 }
 
@@ -327,7 +348,6 @@ mkshort(void *h, const char *istring)
        if (hdl->must_uniq) {
                return mkshort_add_to_list(hdl, ostring);
        }
-
        return ostring;
 }